<?php //install step 5- Write Configuration File
define('DB_DNC', true);
require_once('../inc/inc.common.php');

$tmpl 		= new mvTemplate();

if($_POST['submit']){
	// unless we've come from somewhere else
	$fn = $_POST['install_first_name'];
	$ln = $_POST['install_last_name'];
	$e = $_POST['install_email'];
	$p1= $_POST['install_password1'];
	$p2 = $_POST['install_password2'];
}

$install_success = true;
$error = array();

// Check for a first name.
if(trim($fn) != '') {
	$firstname = TRUE;
}else{
	$firstname = FALSE;
 	$error[] = 'First name not entered!';
}

// Check for a last name.
if(trim($ln) != '') {
	$lastname = TRUE;
}else{
	$lastname = FALSE;
	$error[] = 'Last name not entered!';
}

// Check for an email address.
if(trim($e) != '') {
	$email = TRUE;
}else{
	$email = FALSE;
	$error[] = 'Email Address not entered!';
}

// Check for a password and match against the confirmed password.
if(trim($p1) != '') {
	if ($p1 == $p2) {
		$password = TRUE;
	}else{
		$password = FALSE;
		$error[] = 'Your password did not match the confirm password!';
	}
}else{
	$password = FALSE;
	$error[] = 'Invalid password!';
}

if($firstname && $lastname && $email && $password) { // If everything's OK.
	if(!class_exists('mvPDO'))
		require_once(POMBY_DIR . '/classes/class.mvPDO.php');
	if(!class_exists('mvPDOStatement'))
		require_once(POMBY_DIR . '/classes/class.mvPDOStatement.php');

	try{
		$newdb = new mvPDO('mysql:host='.mvSettings::get('db_host').';dbname='.mvSettings::get('db_name'), mvSettings::get('db_user'), mvSettings::get('db_password'), array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));
		$newdb->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
		$newdb->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('mvPDOStatement', array()));
	}catch(Exception $e){
		die('Could not connect to database, please contact technical support.  ' . print_r($e, true));
	};

	$sql = 'INSERT INTO authors (UserName, FirstName, LastName, Email, Approved, Passwd, RegistrationDate) VALUES (?, ?, ?, ?, ?, PASSWORD(?), NOW())';
	$parms = array('Admin', $fn, $ln, $e, 'Y', $p1);
	if(!$newdb->prepare_execute($sql, $parms)){
		$install_success = false;
	}
}else{
	$install_success = false;
}

if($install_success){
	// lets output everything to the file we are done
	mvSettings::set('kbinstall', 1);
	mvSettings::save(POMBY_DIR . '/config/config.php');
}

$tmpl->assign('install_success', $install_success);
$tmpl->assign('error', $error);
$tmpl->page_install('install_step5.tpl');

?>